package com.icinfo.cloud.provider.punish.common.electricsent.controller;


import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.electricsent.SentReceiptDocQueryDto;
import com.icinfo.cloud.provider.punish.common.electricsent.dto.*;
import com.icinfo.cloud.provider.punish.common.electricsent.service.IDocumentSentFlowService;
import com.icinfo.cloud.provider.punish.common.electricsent.vo.CaseDocumentSendinfoConfirmVo;
import com.icinfo.cloud.provider.punish.common.electricsent.vo.DocumentSendRecordByPartiesVo;
import com.icinfo.cloud.provider.punish.common.electricsent.vo.DocumentSentFlowDetailInfoVo;
import com.icinfo.cloud.provider.punish.common.electricsent.vo.DocumentSentRegisterVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：文书送达流程前端控制器
 *
 * @author xiangyuyu
 * @date 2022/2/18 14:46
 */
@RestController
@RefreshScope
@RequestMapping("/punish/common/electric/sent/flow")
@Slf4j
public class DocumentSentFlowController extends BaseController {
    @Resource
    private IDocumentSentFlowService documentSentFlowService;

    /**
     * 【V1.0.0】描述：获取送达流程详情数据
     *
     * @param queryDto {@link DocumentSentFlowDetailInfoQueryDto}
     * @return {@link Result <DocumentSentFlowDetgetDocumentSentFlowDetailInfoailInfoVo>} 送达流程详情数据
     * @author xiangyuyu
     * @date 2022-02-16 16:02
     */
    @PostMapping("/getDocumentSentFlowDetailInfo")
    @ApiOperation(value = "【V1.0.0】描述：获取送达流程详情数据", httpMethod = "POST")
    public Result<DocumentSentFlowDetailInfoVo> getDocumentSentFlowDetailInfo(@RequestBody @Validated DocumentSentFlowDetailInfoQueryDto queryDto) {
        DocumentSentFlowDetailInfoVo vo = documentSentFlowService.getDocumentSentFlowDetailInfo(queryDto);
        return Result.success(Constant.STRING_GET_SUCCESS, vo);
    }

    /**
     * 【V1.0.0】描述：确认送达（根据当事人）
     *
     * @param saveDto {@link ConfirmSendSaveDto}
     * @return {@link Result<String>} 送达结果
     * @author xiangyuyu
     * @date 2022-02-16 16:02
     */
    @PostMapping("/confirmSend")
    @ApiOperation(value = "【V1.0.0】描述：获取送达流程详情数据", httpMethod = "POST")
    public Result<String> confirmSend(@RequestBody @Validated ConfirmSendSaveDto saveDto) {
        return documentSentFlowService.confirmSend(saveDto);
    }

    /**
     * 【V2.0.0】描述：获取电子送达页面送达人初始化信息
     *
     * @param request {@link DocumentSentAddressInfoRequest}
     * @return {@link Result<CaseDocumentSendinfoConfirmVo>} 电子送达页面送达人初始化信息
     * @author xiangyuyu
     * @date 2022-02-28 15:58
     */
    @PostMapping("/getReceiverInitInfo")
    @ApiOperation(value = "【V2.0.0】描述：获取电子送达页面送达人初始化信息", httpMethod = "POST")
    public Result<CaseDocumentSendinfoConfirmVo> getReceiverInitInfo(@RequestBody @Validated DocumentSentAddressInfoRequest request) {
        CaseDocumentSendinfoConfirmVo vo = documentSentFlowService.getReceiverInitInfo(request);
        return Result.success(Constant.STRING_GET_SUCCESS, vo);
    }


    /**
     * 【V1.1.0】描述：获取送达回证文书地址
     *
     * @param queryDto {@link SentReceiptDocQueryDto}
     * @return String 送达回证文书地址
     * @author xiangyuyu
     * @dque 2022-04-20 14:40
     */
    @PostMapping("/getSentReceiptDocUrl")
    @ApiOperation(value = "【V1.1.0】描述：获取送达回证文书地址", httpMethod = "POST")
    public Result<String> getSentReceiptDocUrl(@Validated @RequestBody SentReceiptDocQueryDto queryDto) {
        String url = documentSentFlowService.getSentReceiptDocUrl(queryDto);
        if (ObjectUtils.isEmpty(url)) {
            return Result.error("未获取到送达回证文书信息");
        } else {
            return Result.success(Constant.STRING_GET_SUCCESS, url);
        }
    }

    /**
     * 【V1.1.0】描述：现场送达状态标记
     *
     * @param saveDto {@link RealPlaceSentInfoSaveDto}
     * @return Result<String> 操作结果
     * @author xiangyuyu
     * @dque 2022-04-24 16:08
     */
    @PostMapping("/signRealPlaceSentInfo")
    @ApiOperation(value = "【V1.1.0】描述：现场送达状态标记", httpMethod = "POST")
    public Result<String> signRealPlaceSentInfo(@Validated @RequestBody RealPlaceSentInfoSaveDto saveDto) {
        Map<String, Object> resultMap = documentSentFlowService.signRealPlaceSentInfo(saveDto);
        // FIXME: 2022/5/6 待优化
        if ("true".equals(resultMap.get("success").toString())) {
            return Result.success(resultMap.get("msg").toString());
        } else {
            return Result.error(resultMap.get("msg").toString());
        }
    }

    /**
     * 【V1.1.0】描述：送达流程滚动
     *
     * @param saveDto {@link SentFlowRollInfoSaveDto}
     * @return Result<String> 滚动结果
     * @author xiangyuyu
     * @date 2022-05-06 17:21
     */
    @PostMapping("/sentFlowRoll")
    @ApiOperation(value = "【V1.1.0】描述：送达流程滚动", httpMethod = "POST")
    public Result<String> sentFlowRoll(@RequestBody @Validated SentFlowRollInfoSaveDto saveDto) {
        Map<String, Object> map = documentSentFlowService.sentFlowRoll(saveDto);
        if (!ObjectUtils.isEmpty(map) && map.size() > 0) {
            return Result.success("滚动成功");
        } else {
            return Result.error("滚动失败");
        }
    }

    /**
     * 【V1.0.0】描述：保存文书送达登记
     *
     * @param saveDto {@link DocumentSentRegisterSaveDto}
     * @return {@link Result <String>} 保存结果
     * @author xiangyuyu
     * @date 2022-02-16 16:02
     */
    @PostMapping("/saveDocumentSentRegister")
    @ApiOperation(value = "【V1.0.0】描述：保存文书送达登记", httpMethod = "POST")
    public Result<String> saveDocumentSentRegister(@RequestBody @Validated DocumentSentRegisterSaveDto saveDto) {
        return documentSentFlowService.saveDocumentSentRegister(saveDto);
    }

    /**
     * 【V2.0.0】描述：查询文书送达登记信息
     *
     * @param request {@link DocumentSentRegisterInfoRequest}
     * @return {@link Result<DocumentSentRegisterVo>} 文书送达登记信息
     * @author xiangyuyu
     * @date 2022-02-16 16:02
     */
    @PostMapping("/getDocumentSentRegisterInfo")
    @ApiOperation(value = "【V2.0.0】描述：查询文书送达登记信息", httpMethod = "POST")
    public Result<DocumentSentRegisterVo> getDocumentSentRegisterInfo(@RequestBody @Validated DocumentSentRegisterInfoRequest request) {
        DocumentSentRegisterVo vo = documentSentFlowService.getDocumentSentRegisterInfo(request);
        return Result.success(Constant.STRING_GET_SUCCESS, vo);
    }

    /**
     * 查询多当事人文书送达记录
     *
     * @return {@link Result< DocumentSendRecordByPartiesVo>}
     * @author: zhengqiang
     * @date: 2022/5/17
     * @param: queryDto
     **/
    @PostMapping("/getDocumentSendRecordByParties")
    @ApiOperation(value = "【V1.0.0】描述：查询多当事人文书送达记录", httpMethod = "POST")
    public Result<List<DocumentSendRecordByPartiesVo>> getDocumentSendRecordByParties(@RequestBody @Validated DocumentSendRecordByPartiesQueryDto documentSendRecordByPartiesQueryDto) {
        List<DocumentSendRecordByPartiesVo> list = documentSentFlowService.getDocumentSendRecordByParties(documentSendRecordByPartiesQueryDto);
        return Result.success(Constant.STRING_GET_SUCCESS, list);
    }


    /**
     * 描述：发送短信
     *
     * @param dto {@link SendSmsDto}
     * @return 发送结果(错误时返回错误信息 ， 成功返回空值)
     * @author xiangyuyu
     * @date 2022-02-26 13:58
     */
    //@PostMapping("/sendSms")
    @ApiOperation(value = "描述：发送短信", httpMethod = "POST")
    public Result<String> sendSms(@RequestBody @Validated SendSmsDto dto) {
        String content = dto.getContent();
        String mobiles = dto.getMobiles();
        String msg = documentSentFlowService.sendSms(mobiles, content);
        return Result.success(msg);
    }

    /**
     * 描述：发送短信【省级通用】
     *
     * @param dto {@link SendSmsDto}
     * @return 发送结果(错误时返回错误信息 ， 成功返回空值)
     * @author xiangyuyu
     * @date 2022/8/28
     * @since 1.4.1
     */
    //TODO:测试开发，待屏蔽
    @PostMapping("/sendSmsProvince")
    @ApiOperation(value = "描述：发送短信【省级通用】", httpMethod = "POST")
    public Result<String> sendSmsProvince(@RequestBody @Validated SendSmsDto dto) {
        String content = dto.getContent();
        String mobiles = dto.getMobiles();
        String msg = documentSentFlowService.sendSmsProvince(mobiles, content, null);
        return Result.success(msg);
    }

    /**
     * 描述：生成电子送达对应送达回证信息
     *
     * @param dto {@link ConfirmSendSaveDto}
     * @return Result<String> 操作结果
     * @author xiangyuyu
     * @date 2022-06-20 20:13
     */
    @PostMapping("/generateElectricSentReceipt")
    @ApiOperation(value = "生成电子送达对应送达回证信息", httpMethod = "POST")
    public Result<String> generateElectricSentReceipt(@RequestBody @Validated ConfirmSendSaveDto dto) {
        String msg = documentSentFlowService.generateElectricSentReceipt(dto);
        if (ObjectUtils.isEmpty(msg)) {
            return Result.success(Constant.STRING_OPERATE_SUCCESS);
        } else {
            return Result.error(msg);
        }
    }

    /**
     * 【V1.4.1】描述： 根据文书id及当事人信息发送文书短信
     *
     * @return {@link Result< String>}
     * @author: zhengqiang
     * @date: 2022/8/28
     * @param: smsDto
     **/
    @PostMapping("/sendDocumentSms")
    public Result<String> sendDocumentSms(@RequestBody @Validated SendDocumentSmsDto smsDto) {
        return documentSentFlowService.sendDocumentSms(smsDto);
    }

    /**
     * 描述: 浙里办发送短信，生成空白默认的送达地址确认书，待填写状态
     *
     * @param smsDto
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author zhanghongqiang
     * @Date 2022/12/6
     */
    @PostMapping("/sendDocumentSmsZlb")
    @ApiOperation(value = "浙里办发送短信，生成空白默认的送达地址确认书", httpMethod = "POST")
    public Result<String> sendDocumentSmsZlb(@RequestBody @Validated SendDocumentSmsZlbDto smsDto) {
        return documentSentFlowService.sendDocumentSmsZlb(smsDto);
    }

    /**
     * 描述: 撤回浙里办填写送达信息数据
     *
     * @param withdrawZlbDto
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author zhanghongqiang
     * @Date 2022/12/8
     */
    @PostMapping("/withdrawZlb")
    @ApiOperation(value = "描述: 撤回浙里办填写送达信息数据", httpMethod = "POST")
    public Result<String> withdrawZlb(@RequestBody @Validated WithdrawZlbDto withdrawZlbDto) {
        return documentSentFlowService.delWithdrawZlb(withdrawZlbDto);
    }
}
